package mws;

import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController("/controller")
public class ControllerGetNacos {

private final DiscoveryClient discoveryClient;

    @GetMapping("/get-nacos")
    public String getNacos() {
        List<String> services = discoveryClient.getServices();
        StringBuilder stringBuilder = new StringBuilder();

        for (String service : services) {
            System.out.println(service);
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance instance : instances) {
                stringBuilder.append(instance.getHost() + ":" + instance.getPort() +
                        "-----------" + instance.getServiceId() +
                        "-----------" + instance.getUri() +
                        "-----------" + instance.getScheme() +
                        "-----------" + JSON.toJSONString(instance.getMetadata())
                );
            }

        }


        return stringBuilder.toString();
    }



}
